/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iss.basket.bizLogic;

import com.iss.basket.ajaxModels.ProductList;
import com.iss.basket.models.Inventory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author yc
 */
@Stateless
public class ProductService {
    
    @PersistenceContext
    EntityManager em;    
    
   public ProductList getProducts(int currentPage, int resultsPerPage)
   {
       ProductList pList = new ProductList();
       String sql = "select i from Inventory i";
       Query q = em.createQuery(sql);
       q.setFirstResult((resultsPerPage*(currentPage-1)));
       q.setMaxResults(resultsPerPage);  
       pList.setProducts(q.getResultList());
       
       String sqlCount = "select count(i) from Inventory i";
       q=em.createQuery(sqlCount);
       pList.setMaxResults(Integer.parseInt(q.getSingleResult().toString()));
       return pList;
   }
   
   public Inventory getProductDetail(int productId)
   {
       Inventory i = em.find(Inventory.class, productId); //return one product
       return i;
   }
}
